package com.lagou.bak;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Random;

public class WindowTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        String host = "hdp-1";
        int port = 7777;
        DataStreamSource<String> data = env.socketTextStream(host, port);
        DataStreamSource<String> data1 = env.addSource(new SourceFunction<String>() {
            int count = 0;

            @Override
            public void run(SourceContext<String> ctx) throws Exception {

                while (true) {
                    ctx.collect(count++ + " ");
                    Thread.sleep(1000);
                }
            }

            @Override
            public void cancel() {

            }
        });
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss.SSS");
        SingleOutputStreamOperator<Tuple3<String,String,Integer>> maped = data.map(new MapFunction<String, Tuple3<String,String,Integer>>() {
            @Override
            public Tuple3<String,String,Integer> map(String value) throws Exception {
                long cu = System.currentTimeMillis();

                String currentTime = sdf.format(cu);
                Random random = new Random();
                int count = random.nextInt(10);

                return new Tuple3<String,String,Integer>(value,currentTime,count);
            }
        });

        KeyedStream<Tuple3<String,String,Integer>, String> keyByed = maped.keyBy(value -> value.f0);
        WindowedStream<Tuple3<String,String,Integer>, String, TimeWindow> timeWindowed = keyByed.timeWindow(Time.seconds(3));
        SingleOutputStreamOperator<String> applyed = timeWindowed.apply(new WindowFunction<Tuple3<String, String, Integer>, String, String, TimeWindow>() {
            int count = 0;
            @Override
            public void apply(String s, TimeWindow window, Iterable<Tuple3<String, String, Integer>> input, Collector<String> out) throws Exception {

//                System.out.println("s:" + s + "...window.start:" + sdf.format(window.getStart()) + "...");
                Iterator<Tuple3<String, String, Integer>> iterator = input.iterator();
                StringBuilder sb = new StringBuilder();
                while(iterator.hasNext()) {
                    Tuple3<String, String, Integer> next = iterator.next();
                    String s1 = next.f0 + "..." + next.f1 + "..." + next.f2;
                    sb.append(s1);
                }

                out.collect(sb + "...from out ... s:" + s + "...window.start:" + sdf.format(window.getStart()) + "...");
            }
        });


        applyed.print();
        env.execute();

    }
}
